package com.controller.home;

import com.controller.BaseController;
import com.dao.UserAddressMapper;
import com.dao.VipUserMapper;
import com.entity.ShopOrder;
import com.entity.UserAddress;
import com.entity.VipUser;
import com.service.imple.home.ShopOrderServiceImple;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**购买**/

@Controller
@RequestMapping("home/ShopOrder")
public class ShopOrderController extends BaseController<ShopOrderServiceImple> {
    @Autowired
    private VipUserMapper vipUserMapper;

    /**填写订单信息**/
    @RequestMapping("ShopOrder")
    @ResponseBody
    public Map<String, Object> pay(@SessionAttribute("userId")Integer userId, String accept_name, String province, String city, String area, String address, String email,
                                   String post_code, String message, BigDecimal totalAmount, String telphone, HttpServletRequest request){
        ShopOrder shopOrder = new ShopOrder();
        shopOrder.setAddress(address);//详细地址
        shopOrder.setAddTime(new Date());//时间
        shopOrder.setArea(area);//区
        shopOrder.setCity(city);//市
        shopOrder.setEmail(email);//邮箱
        shopOrder.setCode(post_code);//邮政编码
        shopOrder.setPhone(telphone);//手机号码
        shopOrder.setUserName(accept_name);//姓名
        shopOrder.setProvince(province);//省
        shopOrder.setTotalMoney(totalAmount);//总金额
        shopOrder.setRemark(message);//备注
        shopOrder.setShopState(0);
        shopOrder.setUserId(userId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateString = sdf.format(new Date());
        shopOrder.setOrderSn(dateString);

        int count = BaseService.insert(shopOrder);


        Map<String,Object> map = new HashMap<>();
        if (count==1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        System.out.println(map);
        return map;
    }

    @RequestMapping("orderList")
    public String orderList(@SessionAttribute("userId")Integer userId, ModelMap modelMap){
        ShopOrder shopOrder = new ShopOrder();
        shopOrder.setUserId(userId);
        List<ShopOrder> list = BaseService.SelectAll(shopOrder);
        modelMap.put("list",list);
        return "center/order/orderlist";
    }

    /**
     * 修改订单状态
     * @return
     */
    @RequestMapping("shopState")
    public String shopState(String orderSn,BigDecimal totalMoney,Integer userId){
        ShopOrder shopOrder = new ShopOrder();
        shopOrder.setOrderSn(orderSn);
        BaseService.Delete(shopOrder);
        VipUser vipUser = new VipUser();
        vipUser.setId(userId);
        List<VipUser> list1 = vipUserMapper.selectAll(vipUser);
        list1.get(0).setRechargeMoney(totalMoney);
        vipUserMapper.updateByPrimaryKey(list1.get(0));
        return "center/order/orderDefault";
    }


}
